package org.demo.base.config;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormateUtil {
	public static final String DATE_FORMATE = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_FORMATE_YMD = "yyyyMMdd";

	public static String getFormateDate() {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMATE);
		return sdf.format(d);
	}

	public static String getFormateDate(Date d) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMATE);
		return sdf.format(d);
	}

	public static String getYMDDate() {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMATE_YMD);
		return sdf.format(d);
	}

	/**
	 * 把日期对象根据生成指定格式的字符串
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String formatDate(Date date, String format) {
		String result = "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		if (date != null) {
			result = sdf.format(date);
		}
		return result;
	}

	/**
	 * 把日期字符串生成指定格式的日期对象
	 * 
	 * @param str
	 * @param format
	 * @return
	 * @throws Exception
	 */
	public static Date formatString(String str, String format) throws Exception {
		if (str == null || str.equals("")) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(str);
	}

	/**
	 * 生成当前年月日字符串
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String getCurrentDateStr() throws Exception {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMATE);
		return sdf.format(date);
	}

	/**
	 * 当前日期加上天数后的日期
	 * 
	 * @param num 为增加的天数
	 * @return
	 */
	public static Date plusDay(int num) {
		Date date = new Date();
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
		date = ca.getTime();
		return date;
	}
}
